<?php
// Copyright (C) 2011 Cassian LUP <cassi.lup@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//SANITIZE ALL ESCAPES

$sanitize_all_escapes = true;

//STOP FAKE REGISTER GLOBALS
$fake_register_globals = false;
//continue session
session_start();

//landing page definition -- where to go if something goes wrong
$landingpage = "index.php?site=" . $_SESSION['site_id'];
//
// kick out if patient not authenticated
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    session_destroy();
    header('Location: ' . $landingpage . '&w');
    exit;
}

//../encounter/trend_form.php?formname=vitals

$ignoreAuth = true;
require_once('../../interface/globals.php');

$data = categoria_paciente();
$dataCategoriasHTML = "";
if (sizeof($data) > 0) {
    foreach ($data as $valor) {
        $dataCategoriasHTML .= "<option value='" . $valor["id"] . "'>" . $valor["name"] . "</option>";
    }
}

function categoria_paciente() {
    ob_start();
    $query = "SELECT * FROM categories WHERE parent = 99999999";
    $res = sqlStatement($query);
    if ($res) {
        for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
            $all[$iter] = $row;
        }
    }
    $v = ob_get_clean();
    return $all;
}

//print_r($data);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>paciente uploader</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <link href="css/bootstrap.css" rel="stylesheet" />
        <link href="css/tQera.Uploader.Bootstrap.css" rel="stylesheet" />
        <link href="css/app.css" rel="stylesheet" />
        <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/nightingale.css" type="text/css"/>
    </head>
    <body>
        <div class="container">

            <div class="titulo_uploader">
                <span>Herramienta para adjuntar archivos al expediente m&eacute;dico</span>
            </div>

            <form class="row-fluid" id="dropper">
                <div class="text-center">
                    <input id="fileInput" name="fileInput" type="file" class="btn btn-file hide" multiple />
                </div>
                <div style="padding-bottom: 5px">
                </div>
                <div class="row-fluid text-center">
                    <div class="span12 drop-zone" id="dropPlace">
                    </div>
                </div>
                <div id="message" class="message" style="display: none">
                    <span>Debe escoger una categoria para los archivos</span>
                </div>
                <div id="message2" class="message" style="display: none">
                    <span>Archivo subido correctamente</span>
                </div>
                <table border="0">
                    <tr>
                        <td width="150px">
                            <span class="upload_categoria">Categoria del documento:</span>
                        </td>
                        <td width="230px">
                            <select id="drpCategoria" class="upload_categoria">
                                <option value="-1">Seleccione...</option>
                                <?php echo $dataCategoriasHTML; ?>
                            </select>
                        </td>
                        <td>
                            <button type="submit" class="boton_normal"><i class="icon-white icon-arrow-up"></i>Subir archivos</button>
                        </td>
                    </tr>
                </table>
                <div class="row-fluid images" id="imageHolder">
                </div>
            </form>
            <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
            <script type="text/javascript" src="js/tQera.Image.Uploader.js"></script>
            <script type="text/javascript" src="js/bootstrap.js"></script>
            <script>
                var d = new tQEraUploader(
                        {
                            drop: true,
                            imageHolder: document.getElementById("imageHolder"),
                            dragHoverClass: "drop_hover",
                            image_thumb_width: 24,
                            image_thumb_height: 24,
                            dropPlace: document.getElementById("dropPlace"),
                            form: document.getElementById("dropper"),
                            fileInput: document.getElementById("fileInput"),
                            file_closebutton_class: "btn btn-danger close",
                            file_class: "list-i",
                            file_filter: "",
                            image_thumb: false,
                            icon_path: "FileIcons/",
                            icon_default: "FileIcons/_blank.png",
                            limit: 0,
                            ajax: {
                                url: 'Handler.php',
                                clearAfterUpload: true
                            },
                            watermark: {
                                text: ""
                            },
                            html5Error:
                                    function(uploader) {

                                        uploader.settings.imageHolder.style.display = "none";
                                        //document.getElementById("dropper").removeChild(imageholder);

                                        uploader.settings.dropPlace.style.display = "none";
                                        var error = document.createElement("p");
                                        error.className = "text-center";
                                        error.appendChild(document.createTextNode("Your browser doesn't support HTML5, we can offer you a new browser from here ! click!"));
                                        uploader.settings.form.appendChild(error);
                                    },
                            progress:
                                    function(data) {
                                        var template = document.getElementById(data.template);
                                        console.log(data.template);
                                        if (template) {
                                            var progress = document.getElementById("progress_" + data.template);

                                            if (progress) {
                                                progress.style.width = data.percent + "%";
                                            }
                                            else {
                                                var div = document.createElement("div");
                                                div.className = "progress progress-striped active";

                                                progress = document.createElement("div");
                                                progress.id = "progress_" + data.template;
                                                progress.className = "bar";
                                                progress.style.width = data.percent + "%";
                                                div.appendChild(progress);

                                                template.appendChild(div);
                                            }
                                        }

                                    },
                            success:
                                    function(event) {
                                        console.log("Its uploaded ");
                                    }
                        });
            </script>
        </div>
    </body>
</html>
